package com.lemner.motion.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemner.motion.R;
import com.lemner.motion.activity.ChangeUserActivity;
import com.lemner.motion.activity.LoginActivity;
import com.lemner.motion.activity.UserSettingActivity;
import com.lemner.motion.base.BaseFragment;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.io.File;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link UserFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class UserFragment extends BaseFragment implements View.OnClickListener {

    private OnFragmentInteractionListener mListener;
    private TextView tv_studentnum,tv_studentname;
    private ImageView iv_studentpic,iv_studentgender,iv_right;
    private  String studentname,studentid,studentsex, headpic;
    private LinearLayout ll_setting;
    public UserFragment() {
        // Required empty public constructor
    }
    @Override
    public int getLayoutFile() {
        return R.layout.fragment_user;
    }
    @Override
    public void initView() {
        tv_studentnum=(TextView)view.findViewById(R.id.tv_studentnum);
        tv_studentname=(TextView)view.findViewById(R.id.tv_studentname);
        iv_studentpic=(ImageView)view.findViewById(R.id.iv_headpic);
        iv_studentgender=(ImageView)view.findViewById(R.id.iv_gender);
        iv_right=(ImageView)view.findViewById(R.id.iv_right);
        ll_setting=(LinearLayout)view.findViewById(R.id.ll_setting);

    }
    @Override
    public void initEvent() {
        iv_studentpic.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        ll_setting.setOnClickListener(this);
    }
    @Override
    public void initData() {
        studentid=getArguments().getString("studentid");
        studentname =getArguments().getString("studentname");
        tv_studentname.setText(studentname);
        String studentnum=getArguments().getString("studentnum");
        tv_studentnum.setText(studentnum);
        studentsex=getArguments().getString("studentsex");
        if(null!=studentsex&&studentsex.equals("0"))
            iv_studentgender.setImageResource(R.drawable.women);
        headpic=getArguments().getString("studentpic");
        initPic();

    }
    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    public  void initPic(){
        if (headpic != null && !headpic.isEmpty())
            Picasso.with(getActivity()).load(headpic).resize(100,100).transform(new Transformation() {
                @Override
                public Bitmap transform(Bitmap source) {
                    int size = Math.min(source.getWidth(), source.getHeight());
                    int x = (source.getWidth() - size) / 2;
                    int y = (source.getHeight() - size) / 2;
                    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
                    if (squaredBitmap != source) {
                        source.recycle();
                    }
                    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
                    Canvas canvas = new Canvas(bitmap);
                    Paint paint = new Paint();
                    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
                    paint.setShader(shader);
                    paint.setAntiAlias(true);

                    float r = size / 2f;
                    canvas.drawCircle(r, r, r, paint);
                    squaredBitmap.recycle();
                    return bitmap;
                }
                @Override
                public String key() {
                    return "circle()";
                }
            }).placeholder(R.drawable.pic).into(iv_studentpic);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_right:
                Intent intent=new Intent(getActivity(), ChangeUserActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_setting:
                Intent intent1=new Intent(getActivity(), UserSettingActivity.class);
                intent1.putExtra("studentid",studentid);
                startActivity(intent1);
        }

    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }



}
